<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Search_model extends CI_Model{
    
    public function __construct() {
        parent::__construct();
    }
    
    /**
     * Быстрый поиск по ключевому слову
     * @param string $str
     * @return string JSON
     */
    public function live_search($str){
        $sql = "SELECT DISTINCT SUBSTR(t.PURE_PATH, instr(t.PURE_PATH,'/',-1)+1) as SHORT_NAME FROM ".$this->config->item('resolv_table')." t WHERE t.PURE_PATH LIKE '%.sh' AND UPPER(substr(t.PURE_PATH, instr(t.PURE_PATH,'/',-1))) LIKE Upper( ? ) ORDER by 1"; //
        $query = $this->db->query($sql,array('%'.$this->db->escape_like_str($str).'%'));
        $json = array();
        foreach ($query->result_array() as $row){
            $json[] = substr($row['SHORT_NAME'],0,count($row['SHORT_NAME'])-4);
        }
        return json_encode($json);
    }
    
    /**
     * Полный поиск всех элементов
     * @param syting $word
     * @param boolean $full_scan
     * @return array BinaryNode
     */
    private function full_search($word,$full_scan=FALSE){
        $sql = "SELECT t.* FROM ".$this->config->item('resolv_table')." t WHERE t.PURE_PATH LIKE '%.sh' AND ";
        if($full_scan){
            $sql .= "UPPER(t.PURE_PATH) LIKE UPPER( ? )";
        }else{
            $sql .= "UPPER(substr(t.PURE_PATH, instr(t.PURE_PATH,'/',-1))) LIKE UPPER( ? )";
        }
        $query = $this->db->query($sql,array('%'.urldecode($word).'%'));
        $res=array();
        $i=0;
        foreach ($query->result_array() as $row) {
            $res[$i++] = new BinaryNode($row);
        }
        return $res;
    }
    
    /**
     * Получить список BinaryNode элементов для построения графиков
     * @param string $search_word
     * @return array BinaryNode
     */
    public function get_ary_for_charts($search_word){
        $result = array();
        if(strlen($search_word) > 2){
            $ary = $this->full_search($search_word);
            foreach ($ary as $node_obj) {
                $result[$node_obj->get_hash()] = $node_obj;
            }
        }
        return $result;
    }
    
    /**
     * Вывод результатов поиска
     * @param string $search_word
     * @return string HTML result
     */
    public function show_search_result($search_word){
        $list = "Ничего не найдено";
        if(strlen($search_word) < 3) return $list;
        $search_word = urlencode($search_word);
        $ary = $this->full_search($search_word,false);
        
        if(count($ary) <= 0) return $list;
        
        if(count($ary) <= $this->config->item('search_max_result')){
            $list = "<a href='/chart/search/" . $search_word . "'>Построить все графики</a><br>\n";
        }else{ 
            $list = "";
        }
        
        $list .= '<ul style="display:block;" class="ul_item">';
        foreach ($ary as $node_obj) {
            $list .= sprintf('<li class="file" id="%1$s"><img width="16" height="16" class="fav hand" align="bottom" title="Добавить в закладки" alt="Добавить в закладки" src="/images/star-small.gif">'.
                    '<a title="%2$s" href="/chart/node/%1$s">%2$s</a></li>'."\n", $node_obj->get_hash(), $node_obj->get_title_full() );
        }
        $list .= "</ul>";
        return $list;
    }
}
